package com.lhy.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lhy.domain.ChatTea;
import com.lhy.domain.Result;
import com.lhy.domain.Student;
import com.lhy.domain.Tea;
import com.lhy.service.IUserService;

@Controller
@RequestMapping("/teashopping")
public class TeaShoppingController {
	@Autowired
	private IUserService userService;
	@RequestMapping("")
	public ModelAndView list(){
		ModelAndView mv = new ModelAndView();
		try {
			List<Tea> list = new ArrayList<>();
			list = userService.selectTeas();
			mv.addObject("list",list);
			mv.setViewName("teashopping");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mv;
	}
	@RequestMapping("/do")
	public @ResponseBody
	Map<String, Object> add(HttpSession session,String sequence, String teaId)  {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		Result result=new Result();
		result.setCode("1");
		result.setDes("成功");
		
		try{
			Integer count = userService.queryAllShopCartCountBysequence(Integer.valueOf(sequence));
			
			Tea tea = userService.queryTeaByteaId(Integer.valueOf(teaId));
			userService.insertShoppingCart(Integer.valueOf(sequence),tea.getTeaId(),tea.getTeaname(),tea.getImg(),tea.getUrl(),count+1);
		}catch(Exception e){
				result.setCode("0");
				result.setDes(e.getMessage());
			
		}
		returnMap.put("result", result);
		
		return returnMap;
	}
}
